package au.edu.unsw.infs3634.gamifiedlearning;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import au.edu.unsw.infs3634.gamifiedlearning.API.Country;
import au.edu.unsw.infs3634.gamifiedlearning.API.Name;

public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.CountryViewHolder> implements Filterable {
    private List<Country> mCountry;
    private RecyclerviewClickListener mListener;
    private List<Country> mCountryFiltered;
    private String TAG;
    private String name;

    public CountryAdapter(List<Country> countries, RecyclerviewClickListener listener){
        mCountry = countries;
        mListener = listener;
        mCountryFiltered = countries;
    }

    public interface RecyclerviewClickListener{
        void onClick(View view, String id);
    }

    @NonNull
    @Override
    public CountryAdapter.CountryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.country_list_row, parent, false);
        return new CountryViewHolder(view, mListener);
    }

    @Override
    public void onBindViewHolder(CountryAdapter.CountryViewHolder holder, int position) {
        Country country = mCountryFiltered.get(position);

        holder.mName.setText(country.getName().getCommon());
        holder.itemView.setTag(country.getName().getCommon());
        Glide.with(holder.mImage.getContext())
                .load(country.getFlags().getPng())
                .error(R.mipmap.ic_load_error)
                .placeholder(R.mipmap.ic_load_default)
                .into(holder.mImage);

    }

    @Override
    public int getItemCount() {
        return mCountryFiltered.size();
    }

    public static class CountryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView mName;
        private RecyclerviewClickListener mListener;
        public ImageView mImage;

        public CountryViewHolder(@NonNull View itemView, RecyclerviewClickListener listener) {
            super(itemView);
            mListener = listener;
            itemView.setOnClickListener(this);
            mName = itemView.findViewById(R.id.tvCountryName);
            mImage = itemView.findViewById(R.id.ivCountryFlag);
        }
        @Override
        public void onClick(View v) {
            mListener.onClick(v, (String) v.getTag());
            Log.d("Test",(String) v.getTag());
        }
    }
    public void setData(List<Country> countries){
        mCountryFiltered.clear();
        mCountryFiltered.addAll(countries);
        notifyDataSetChanged();
    }

    //search
    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                String charString = constraint.toString();
                if(charString.isEmpty()) {
                    mCountryFiltered = mCountry;
                } else {
                    ArrayList<Country> filteredList = new ArrayList<>();
                    for(Country country : mCountry) {
                        if(country.getName().getCommon().toLowerCase().contains(charString.toLowerCase())&& !filteredList.contains(country)) {
                            filteredList.add(country);
                            Log.d("common", country.getName().getCommon());
                        }
                        if(country.getName().getOfficial().toLowerCase().contains(charString.toLowerCase())&& !filteredList.contains(country)) {
                            filteredList.add(country);
                            Log.d("official", country.getName().getCommon());
                        }
                    }
                    mCountryFiltered = filteredList;
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = mCountryFiltered;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                mCountryFiltered = (ArrayList<Country>) results.values;
                Log.d("FilterResults", String.valueOf(mCountryFiltered));
                notifyDataSetChanged();
            }
        };
    }
    // Sort method
    public void sort(final int sortMethod) {
        if(mCountryFiltered.size() > 0) {
            Collections.sort(mCountryFiltered, new Comparator<Country>() {
                @Override
                public int compare(Country o1, Country o2) {
                    //Name (A-Z)
                    if (sortMethod == 1) {
                        Log.d("1", "");
                        return o1.getName().getCommon().compareTo(o2.getName().getCommon());
                    } else if (sortMethod == 2) {
                        Log.d("2", "");
                        return o2.getName().getCommon().compareTo(o1.getName().getCommon());
                    } else {
            }
                    return o1.getName().getCommon().compareTo(o2.getName().getCommon());}
                });
        }
        notifyDataSetChanged();
    }
}
